using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace AspWebUtilities.Farm
{
	public partial class FarmData
	{
		Object value = null;

		String DataName;
		UInt64 version = 0;
		Boolean lockedByServer = false;
		
		public FarmData(Object vValue,String vName)
		{
			DataName = vName;
			Boolean isLockedByMe = false;
			SaveData(vName, vValue,ref version,ref isLockedByMe);

			if (isLockedByMe)
			{
				lockedByServer = true;
			}
			value = vValue;
		}

		public FarmData(String vName)
		{
			DataName = vName;
            try
            {
                LoadData(DataName, ref value, ref lockedByServer, ref version);
            }
            catch (Exception ex)
            {
                value = null;
            }
		}

		public Boolean Locked
		{
			get
			{
				if (lockedByServer)
					return true;
				else
				{
					String ServerDnsName = "";
					Boolean ll = IsLocked(DataName, ref ServerDnsName);
					if (ll)
					{
						if (ServerDnsName == System.Net.Dns.GetHostName())
						{
							lockedByServer = true;
						}
					}
					return ll;
				}
			}
		}

		public Boolean LockedByServer
		{
			get
			{
				if (lockedByServer)
				{
					return true;
				}
				else
				{
					String ServerDnsName = "";
					Boolean bb = IsLocked(DataName, ref ServerDnsName);
					if (bb)
					{
						if (ServerDnsName == System.Net.Dns.GetHostName())
						{
							lockedByServer = true;
						}
					}
					return lockedByServer;
				}
			}
		}

		public Object Value
		{
			get
			{
                try
                {
                    if (version < LastUpdate(DataName))
                    {
                        ReloadData();
                    }
                    return value;
                }
                catch (DataDoesntExistsException ex)
                {
                    return null;
                }
			}
			set
			{
				UInt64 lastU = 0;
				Boolean isLockedByMe = false;
				SaveData(DataName, value, ref lastU, ref isLockedByMe);

				lockedByServer = isLockedByMe;

				if (lastU != 0)
				{
					this.value = value;
					version = lastU;
				}
			}
		}

		public void ReloadData()
		{
			LoadData(DataName,ref value,ref lockedByServer, ref version);
		}

		public Boolean LockForServer()
		{
			if (LockedByServer)
			{
				return true;
			}
			else
			{
				if (LockData(DataName))
				{
					lockedByServer = true;
					return true;
				}
				return false;// the Action has finished normaly
			}
		}

		public Boolean UnlockForServer()
		{
			if (lockedByServer)
			{
				if (UnLockData(DataName))
				{
					lockedByServer = false;
					return true;
				}
			}
			return true;// the Action has finished normaly
		}

		public Boolean Lock()
		{
			Boolean res = false;
 
			if (!lockedByServer)
			{
				if (LockData(DataName))
				{
					lockedByServer = true;
					res = true;// the Action has finished normaly
				}
			}
			res = false;
			return res;
		}

		public Boolean Unlock()
		{
			if (UnLockData(DataName))
			{
				lockedByServer = false;
				return  true;// the Action has finished normaly
			}
			return true;
		}

		public void Remove()
		{
			RemoveData(DataName);
		}

		public void Update()
		{
			try
			{
				UInt64 uu = LastUpdate(DataName);

				if (uu > version)
					ReloadData();
			}
			catch (DataDoesntExistsException e)
			{
				value = null;
			}
		}

		public void Update(Object vv)
		{
			Boolean isLockedByMe = false;
			SaveData(DataName, vv, ref version,ref isLockedByMe);
			lockedByServer = isLockedByMe;
			value = vv;
		}

		public static void Remove(String vName)
		{
			RemoveData(vName);
		}

		public static void UnLockAll()
		{
			UnLockAllDatas();
		}

		public static void UnLockAllOwne()
		{
			UnLockAllOwneDatas();
		}

	}
}
